'use strict';

angular.module('lgOauth', ['toaster', 'ui.router'])
.controller('OauthCtrl', function($http, $state, $stateParams, OauthService){

	var self = this;
	self.alerts = [];

	$http.get('/wechat/oauth?code='+ $stateParams.code + '&state='+$stateParams.state).success(function(token){
		if(!OauthService.getToken()){
			OauthService.setToken(token);
		}
		console.log(OauthService.getToken());
		self.alerts.push(token);
		console.log($stateParams);
		var params = $stateParams.state.split('S');
		self.alerts.push(params);
		if(params.length < 1){
		
		} else if(params.length == 1){
			$state.go(params[0]);
		}else {
			$state.go(params[0], {id:params[1], gymId: params.length > 2 ? params[3] : 0});
		}

	});

	console.log(OauthService.getToken());
	console.log($state.current);

})
.controller('LoginCtrl', function($scope, $state, $http, $uibModalInstance, OauthService){

	var sent = false;

	$scope.isSent = function(){
		return sent;
	};


	//var token = OauthService.getToken();

	$scope.bind = function(mobile, passcode){
		$uibModalInstance.close({mobile:mobile, passcode: passcode});

	};

	$scope.passcode = function(mobile){
		$http.get('/member/passcode?mobile='+ mobile)
			.success(function(resp){
				if('8000'===resp.errCode){
					sent = true;
					//toaster.pop('success', '验证码已发送');
				} else {
					//toaster.pop('error', '验证码发送失败');
				}
			});
	};

})
.factory('OauthService', function($window, $http, $uibModal, $state, toaster){

	var session; 
	//var token = {openid:'o0Hl0tzOAqiWhkAmgigC06bOa7M4', unionid:'o-1gYs7AnygMKFKrdpi8DIydWVuU'};
	var token;


	function login(current_state){

		if(!token){
			//$state.go('oauth',{code:'12',state:current_state});
			$window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc26b49b516c47e2a&redirect_uri=http%3A%2F%2Fwesugarfree.com%2Fmember%2F%23%2Foauth%2F&response_type=code&scope=snsapi_base&state="+current_state+"#wechat_redirect"
			//token = {openid:'o0Hl0tzOAqiWhkAmgigC06bOa7M4', unionid:'o-1gYs7AnygMKFKrdpi8DIydWVuU'};
		} else {

			$http.get('/member/needMobile?oid='+ token.openid)
				.success(function(resp){
					var need = resp.data;
					if(true === need){
						var modal = $uibModal.open({
							templateUrl:'views/login.html',
							controller:'LoginCtrl',
							size:'md'
						});

						modal.result.then(function(data){

							$http.get('/member/oauth?oid='+ token.openid+'&unionId='+token.unionid +
									'&mobile='+ data.mobile+'&passcode='+ data.passcode)
								.success(function(resp){

									if('8000'===resp.errCode){
										//toaster.pop('success', '登录成功', resp);
										session = resp.data;
										$state.reload();

									} else {
										toaster.pop('danger', '登录失败', resp);
									}
								})

						});

					} else {
						console.log(token);
						$http.get('/member/oauth?oid='+ token.openid+'&unionId='+token.unionid)
							.success(function(resp){
								if('8000'===resp.errCode){
									session = resp.data;
									console.log(resp.data);
									$state.reload();
									//toaster.pop('success', '登录成功', resp);
								} else {
									//toaster.pop('error', '登录失败', resp);
								}
							});
					}
				})
			.error(function(resp){
				console.log(resp);
			});

		}
	}

		function getWithSession(url){
			var urlPlus = url + '&SFSESSION=' + session.SFSESSION + "&uid=" + session.user.id;
			return $http.get(urlPlus);
		}

		return {
			getSession : function() {return session;},
			setSession: function(obj){ session = obj; },
			getToken : function(){return token;},
			setToken : function(obj){ token = obj; },
			getWithSession : getWithSession,
			login: login
		};

	});
